//
//  NSDictionary+Extend.m
//  XLDoctor
//
//  Created by longma on 2018/12/27.
//  Copyright © 2018年 XLH. All rights reserved.
//

#import "UINavigationController+MJUI.h"

@implementation UINavigationController (MJUI)

- (BOOL)mj_containTargetVCWithClassName:(NSString *)className{
    BOOL isContain = false;
        NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:self.viewControllers];
        for (UIViewController *vc in viewControllers) {
            if ([vc isKindOfClass:[NSClassFromString(className) class]]) {
                isContain = true;
                   break; //break一定要加，不加有时候有bug
               }
           }
    return isContain;
}

- (void)mj_removeTargetVCWithClassName:(NSString *)className{
    if(![self mj_containTargetVCWithClassName:className]){
        return;
    }

    MJUIAfter_GCD(1, ^{
        NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:self.viewControllers];
        for (UIViewController *vc in viewControllers) {
            if ([vc isKindOfClass:[NSClassFromString(className) class]]) {
                   [viewControllers removeObject:vc];
                   break; //break一定要加，不加有时候有bug
               }
           }
        self.viewControllers = viewControllers;
    });
    
    
    
}

- (void)mj_pushAndRemoveCurrentViewController:(UIViewController *)viewController animated:(BOOL)animated{
    NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:self.viewControllers];
    [viewControllers removeLastObject];
    [viewControllers addObject:viewController];
    [self pushViewController:viewController animated:animated];
    self.viewControllers = viewControllers;
}

- (void)mj_removeTargetVCWithClass:(Class)mClass{
    
    if(![self mj_containTargetVCWithClass:mClass]){
        return;
    }
    
        NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:self.viewControllers];
        for (UIViewController *vc in viewControllers) {
            if ([vc isKindOfClass:mClass]) {
                   [viewControllers removeObject:vc];
                   break; //break一定要加，不加有时候有bug
               }
           }
        self.viewControllers = viewControllers;
    
    
}

- (void)mj_popViewControllerWithTargetClass:(Class)mClass{
    
    if(![self mj_containTargetVCWithClass:mClass]){
        return;
    }
    
    NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:self.viewControllers];
    for (UIViewController *vc in viewControllers) {
        if ([vc isKindOfClass:mClass]) {
            [self.navigationController popToViewController:vc animated:YES];
            break; //break一定要加，不加有时候有bug
         }
    }
    
}

- (BOOL)mj_containTargetVCWithClass:(Class)mClass{
    BOOL isContain = false;
        NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:self.viewControllers];
        for (UIViewController *vc in viewControllers) {
            if ([vc isKindOfClass:mClass]) {
                isContain = true;
                   break; //break一定要加，不加有时候有bug
               }
           }
    return isContain;
}

- (void)mj_removeRtTargetVCWithClass:(Class)mClass currentVC:(UIViewController *)currentVC{
    
    if(![self mj_containTargetVCWithClass:mClass]){
        return;
    }
    
    NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:self.viewControllers];
    //存在选择受检人
    for (UIViewController *vc in viewControllers) {
        if ([vc isKindOfClass:mClass]) {
                [self.rt_navigationController removeViewController:vc];//移除控制器
            break; //break一定要加，不加有时候有bug
        }
    }
    
}


- (void)mj_pushViewController:(UIViewController *)viewController animated:(BOOL)animated completion:(void (^)(void))completion {
    // 要先进行转场操作才能产生 self.transitionCoordinator，然后才能用 qmui_animateAlongsideTransition:completion:，所以不能把转场操作放在 animation block 里。
    [self pushViewController:viewController animated:animated];
    if (completion) {
        [self mj_animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
            completion();
        }];
    }
}

- (UIViewController *)mj_popViewControllerAnimated:(BOOL)animated completion:(void (^)(void))completion {
    // 要先进行转场操作才能产生 self.transitionCoordinator，然后才能用 qmui_animateAlongsideTransition:completion:，所以不能把转场操作放在 animation block 里。
    UIViewController *result = [self popViewControllerAnimated:animated];
    if (completion) {
        [self mj_animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
            completion();
        }];
    }
    return result;
}
@end
